<template>
  <div class="background">
    <el-card class="login-form-layout">
      <el-form
        autoComplete="on"
        :model="loginForm"
        :rules="loginRules"
        ref="loginForm"
        label-position="left"
      >
        <div style="text-align: center">
          <svg-icon
            icon-class="login-mall"
            style="width: 56px;height: 16px;color: #409EFF"
          ></svg-icon>
        </div>
        <h2 class="login-title color-main">登录</h2>
        <el-form-item prop="username">
          <el-input
            name="username"
            type="text"
            v-model="loginForm.username"
            autoComplete="on"
            placeholder="请输入用户名">
            <span slot="prefix">
              <svg-icon icon-class="user" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :type="pwdType"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.password"
            autoComplete="on"
            placeholder="请输入密码"
          >
            <span slot="prefix">
              <svg-icon icon-class="password" class="color-main"></svg-icon>
            </span>
            <span slot="suffix" @click="showPwd">
              <svg-icon icon-class="eye" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-row :gutter="70" type="flex" justify="space-around">
          <el-col :span="10">
            <el-form-item style="margin-bottom: 60px">
              <el-button
                style="width: 100%"
                type="primary"
                :loading="loading"
                @click.native.prevent="handleLogin"
              >
                登录
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item style="margin-bottom: 60px">
              <el-button
                style="width: 100%"
                type="warning"
                @click.native.prevent="handleStore"
              >
                注册
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { isvalidUsername } from "@/utils/validate";
import login_center_bg from "@/assets/images/login_center_bg.png";
import { setUserInfo, set } from "@/utils/auth";

export default {
  name: "login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error("请输入正确的用户名"));
      } else {
        callback();
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error("密码不能小于3位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername }
        ],
        password: [{ required: true, trigger: "blur", validator: validatePass }]
      },
      loading: false,
      dialogVisible: true,
      pwdType: "password",
      login_center_bg
    };
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    handleReg() {
      this.$router.push({ path: "/reg" });
    },
    handleStore() {
      this.$router.push({ path: "/acceptStore" });
    },
    handleGit() {
      window.location.href = "";
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          console.log("login");
          this.loading = true;
          this.$store
            .dispatch("Login", this.loginForm)
            .then(res => {
              console.log(res);
              this.loading = false;
              this.$router.push({ path: "/" });
            })
            .catch(e => {
              console.log(e);
              this.loading = false;
            });
        } else {
          console.log("参数验证不合法！");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.background {
  width: 100%;
  /* 根据屏幕的大小自适应充满整个屏幕 */
  /* display: -webkit-flex; */
  height: 100vh;
  background-image:url(https://c.wallhere.com/images/35/78/6e53dc31dee3e84a162aa14155d7-1563109.jpg!d);
  background-repeat:no-repeat;
  background-size:100% 100%;
}
.login-form-layout {
  position: absolute;
  left: 0;
  right: 0;
  width: 360px;
  margin: 140px auto;
  border-top: 10px solid #ffffff;
}

.login-title {
  text-align: center;
}

.login-center-layout {
  background: #409eff;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
</style>
